package sub

import (
	"fmt"

	"github.com/spf13/cobra"

	v1pb "quipus/proto/gen/api/v1"
)

func init() {
	RegisterCommonFlags(signinCmd)
	AddCommand(whoamiCmd)
}

var whoamiCmd = &cobra.Command{
	Use:   "whoami",
	Short: "Run the cmd to check login user",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := NewGrpcConnect(QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewAuthServiceClient(qconn)
		ctxWithToken := NewCtxWithToken(QpConfig.Token)

		in := &v1pb.GetAuthStatusRequest{}
		rsp, err := qclient.GetAuthStatus(ctxWithToken, in)
		if err != nil {
			return err
		}

		fmt.Println("URN         UserNAME     NickName       ROLE       TENANT            CREATEDAT            UPDATEDAT")
		fmt.Printf("%s %10s %10s %20s %10s %20s %20s\n", rsp.Urn, rsp.Username, rsp.Nickname, rsp.Role, rsp.TenantName, rsp.CreateTime.AsTime(), rsp.CreateTime.AsTime())
		return nil
	},
}
